package flyweight;

import java.util.HashMap;
import java.util.Map;

public class ChemicalFactory {
	static Map<String, Chemical> chemicals = new HashMap<>();
	static ChemicalFactory factory;
	static {
		factory = new ChemicalFactory();
		chemicals.put("carbon", factory.new ChemicalImpl("Carbon", "C"));
		chemicals.put("sulfur", factory.new ChemicalImpl("Sulfur", "S"));
	}

	// 内部类，控制构造权限仅为对应工厂类持有
	// 将其抽象为接口方便接口（方法）的使用
	class ChemicalImpl implements Chemical {
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getSymbol() {
			return symbol;
		}

		public void setSymbol(String symbol) {
			this.symbol = symbol;
		}

		String name;
		String symbol;

		public ChemicalImpl(String name, String symbol) {
		}
	}

	public static Chemical getChemical(String name) {
		return chemicals.get(name.toLowerCase());
	}
}
